home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter4 / delitem.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  14KB  |  410 lines

  1.  
  2. #include <windows.h>  
  3. #include "delitem.h"  
  4. #include "commctrl.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "TabCtrl_DeleteItem"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. LPCTSTR szImages[]   = { "CIRCLE", "RECTANGLE", "LINES" };
  107. LPCTSTR szMasks[]    = { "CIRCLEM", "RECTANGLEM", "LINESM" };
  108. LPCTSTR szTips[]     = { "Display a circle", "Display a rectangle", "Display lines" };
  109.  
  110. HWND    hTab         = NULL;
  111. FARPROC pDefStatProc = NULL;
  112.  
  113. LRESULT CALLBACK StatProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  114. {
  115. static TC_ITEM item;
  116. static int nSel = 0;
  117.  
  118.    switch( uMsg )
  119.    {
  120.       case WM_PAINT :
  121.             {
  122.                PAINTSTRUCT ps;
  123.  
  124.                BeginPaint( hWnd, &ps );
  125.  
  126.                // Determine which tab is displayed.
  127.                //..................................
  128.                nSel = TabCtrl_GetCurSel( hTab );
  129.  
  130.                if ( nSel > -1 )
  131.                {
  132.                   item.mask = TCIF_PARAM;
  133.                   TabCtrl_GetItem( hTab, nSel, &item );
  134.  
  135.                   // Paint the appropriate figure for the tab.
  136.                   //..........................................
  137.                   switch( item.lParam )
  138.                   {
  139.                      case 1 : Ellipse( ps.hdc, 10, 10, 100, 100 );
  140.                               break;
  141.  
  142.                      case 2 : Rectangle( ps.hdc, 10, 10, 100, 100 );
  143.                               break;
  144.  
  145.                      case 3 : MoveToEx( ps.hdc, 10, 10, NULL );
  146.                               LineTo( ps.hdc, 100, 100 );
  147.                               MoveToEx( ps.hdc, 100, 10, NULL );
  148.                               LineTo( ps.hdc, 10, 100 );
  149.                               break;
  150.                   }
  151.                }
  152.                EndPaint( hWnd, &ps );
  153.             }
  154.             break;
  155.  
  156.       default :
  157.             return( (*pDefStatProc)(hWnd, uMsg, wParam, lParam) );
  158.    }
  159.  
  160.    return( FALSE );
  161. }
  162.  
  163.  
  164. VOID UpdateTabWindow( HWND hWnd, RECT* pRect )
  165. {
  166.    if ( TabCtrl_GetItemCount( hTab ) == 1 )
  167.    {
  168.       SendMessage( hWnd, WM_SIZE, 0, MAKELPARAM( pRect->right, pRect->bottom ) );
  169.       InvalidateRect( hWnd, NULL, TRUE );
  170.    }
  171. }
  172.  
  173.  
  174. int AddImage( HIMAGELIST hList, int nIdx )
  175. {
  176.    HBITMAP hBmp, hMask;
  177.  
  178.    // Load the images for the image.
  179.    //...............................
  180.    hBmp  = LoadBitmap( hInst, szImages[nIdx] );
  181.    hMask = LoadBitmap( hInst, szMasks[nIdx] );
  182.  
  183.    // Add the image to the list.
  184.    //...........................
  185.    nIdx = ImageList_Add( hList, hBmp, hMask );
  186.  
  187.    DeleteObject( hBmp );
  188.    DeleteObject( hMask );
  189.  
  190.    // Return the image index.
  191.    //........................
  192.    return( nIdx );
  193. }
  194.  
  195.  
  196. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  197. {
  198. static RECT    rcDisp, clRect;
  199. static TC_ITEM item;
  200. static HWND    hStatic = NULL;
  201.  
  202.    switch( uMsg )
  203.    {
  204.       case WM_CREATE :
  205.               // Initialize and create the tab control and the static control.
  206.               //..............................................................
  207.               InitCommonControls();
  208.               hTab = CreateWindowEx( 0, WC_TABCONTROL, "", 
  209.                                      WS_CHILD | WS_CLIPSIBLINGS | 
  210.                                      WS_VISIBLE | TCS_FIXEDWIDTH |
  211.                                      TCS_FORCELABELLEFT | TCS_TOOLTIPS,
  212.                                      0, 0, 10, 10, hWnd, 
  213.                                      (HMENU)101, hInst, NULL ); 
  214.  
  215.               hStatic = CreateWindowEx( 0, "STATIC", "", WS_CHILD | WS_VISIBLE, 
  216.                                       0, 0, 10, 10, hWnd, (HMENU)102, 
  217.                                       hInst, NULL );
  218.  
  219.               if ( hTab )
  220.               {
  221.                  HIMAGELIST hList;
  222.  
  223.  
  224.                  SendMessage( hTab, WM_SETFONT, 
  225.                               (WPARAM)GetStockObject( DEFAULT_GUI_FONT ), 0 );
  226.  
  227.                  // Create an image list and associate it.
  228.                  //.......................................
  229.                  hList = ImageList_Create( 16, 15, ILC_COLOR4 | ILC_MASK, 3, 1 );  
  230.                  TabCtrl_SetImageList( hTab, hList );
  231.  
  232.                  // Set the size and spacing for the Tabs.
  233.                  //.......................................
  234.                  TabCtrl_SetItemSize( hTab, 100, 25 );
  235.                  TabCtrl_SetPadding( hTab, 10, 4 ); 
  236.  
  237.                  // Change tool tip delay.
  238.                  //.......................
  239.                  SendMessage( TabCtrl_GetToolTips( hTab ), 
  240.                               TTM_SETDELAYTIME, TTDT_RESHOW, 1000 );
  241.               }
  242.  
  243.               // Subclass the static control to perform painting.
  244.               //.................................................
  245.               if ( hStatic )
  246.                  pDefStatProc = (FARPROC)SetWindowLong( hStatic, GWL_WNDPROC, (LONG)StatProc );
  247.               break;
  248.  
  249.       case WM_SIZE :
  250.               // Retrieve the client rect.
  251.               //..........................
  252.               GetClientRect( hWnd, &clRect );
  253.               GetClientRect( hWnd, &rcDisp );
  254.  
  255.               // Calculate the tab control display area.
  256.               //........................................
  257.               TabCtrl_AdjustRect( hTab, FALSE, &rcDisp );
  258.  
  259.               // Size the tab control.
  260.               //......................
  261.               MoveWindow( hTab, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE );
  262.               MoveWindow( hStatic, rcDisp.left, rcDisp.top, 
  263.                                    rcDisp.right-rcDisp.left, 
  264.                                    rcDisp.bottom-rcDisp.top, TRUE );
  265.               break;
  266.  
  267.       case WM_NOTIFY :
  268.               switch( ((NMHDR*)lParam)->code )
  269.               {
  270.                  case TCN_SELCHANGE :
  271.                         // The user selected a different tab.
  272.                         // Force the window to be painted.
  273.                         //...................................
  274.                         InvalidateRect( hTab, &rcDisp, TRUE );
  275.                         UpdateWindow( hTab );
  276.                         InvalidateRect( hStatic, NULL, TRUE );
  277.                         break;
  278.  
  279.                  case TTN_NEEDTEXT :
  280.                         // Retrieve item information and set tip text.
  281.                         //............................................
  282.                         item.mask = TCIF_PARAM;
  283.                         TabCtrl_GetItem( hTab, ((NMHDR*)lParam)->idFrom, &item );
  284.                         ((LPTOOLTIPTEXT)lParam)->lpszText = (LPTSTR)szTips[item.lParam-1];
  285.                         break;
  286.               }
  287.               break;
  288.  
  289.       case WM_COMMAND :
  290.               switch( LOWORD( wParam ) )
  291.               {
  292.                  case IDM_CIRCLE :
  293.                         item.mask = TCIF_TEXT | TCIF_PARAM | TCIF_IMAGE;
  294.                         item.pszText = "Circle";
  295.                         item.lParam  = 1;
  296.                         item.iImage  = AddImage( TabCtrl_GetImageList( hTab ), 0 );
  297.                         TabCtrl_InsertItem( hTab, TabCtrl_GetItemCount( hTab ), &item );
  298.                         UpdateTabWindow( hWnd, &clRect );
  299.                         break;
  300.  
  301.                  case IDM_RECTANGLE :
  302.                         item.mask = TCIF_TEXT | TCIF_PARAM | TCIF_IMAGE;
  303.                         item.pszText = "Rectangle";
  304.                         item.lParam  = 2;
  305.                         item.iImage  = AddImage( TabCtrl_GetImageList( hTab ), 1 );
  306.                         TabCtrl_InsertItem( hTab, TabCtrl_GetItemCount( hTab ), &item );
  307.                         UpdateTabWindow( hWnd, &clRect );
  308.                         break;
  309.  
  310.                  case IDM_LINES :
  311.                         item.mask = TCIF_TEXT | TCIF_PARAM | TCIF_IMAGE;
  312.                         item.pszText = "Lines";
  313.                         item.lParam  = 3;
  314.                         item.iImage  = AddImage( TabCtrl_GetImageList( hTab ), 2 );
  315.                         TabCtrl_InsertItem( hTab, TabCtrl_GetItemCount( hTab ), &item );
  316.                         UpdateTabWindow( hWnd, &clRect );
  317.                         break;
  318.  
  319.                  case IDM_DELETE :
  320.                         {
  321.                            int nSel = TabCtrl_GetCurFocus( hTab );
  322.  
  323.                            if ( nSel > -1 )
  324.                            {
  325.                               // Delete the tab and related image.
  326.                               //..................................
  327.                               TabCtrl_DeleteItem( hTab, nSel );
  328.                               TabCtrl_RemoveImage( hTab, nSel );
  329.  
  330.                               // Set the selected tab to the next tab or
  331.                               // the previous one if the deleted tab was the
  332.                               // last tab in the tab control.
  333.                               //............................................
  334.                               if ( nSel > TabCtrl_GetItemCount( hTab )-1 )
  335.                                  nSel--;
  336.  
  337.                               if ( nSel > -1 )
  338.                               {
  339.                                  TabCtrl_SetCurSel( hTab, nSel );
  340.  
  341.                                  InvalidateRect( hTab, &rcDisp, TRUE );
  342.                                  UpdateWindow( hTab );
  343.                                  InvalidateRect( hStatic, NULL, TRUE );
  344.                               }
  345.                               else
  346.                                  InvalidateRect( hTab, NULL, TRUE );
  347.                            }
  348.                         }
  349.                         break;
  350.  
  351.                  case IDM_DELETEALL :
  352.                         {
  353.                            HIMAGELIST hList;
  354.  
  355.                            TabCtrl_DeleteAllItems( hTab );
  356.  
  357.                            // Remove all images from the image list.
  358.                            //.......................................
  359.                            hList = TabCtrl_GetImageList( hTab );
  360.                            ImageList_Remove( hList, -1 );
  361.                         }
  362.                         break;
  363.  
  364.                  case IDM_ABOUT :
  365.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  366.                         break;
  367.  
  368.                  case IDM_EXIT :
  369.                         DestroyWindow( hWnd );
  370.                         break;
  371.               }
  372.               break;
  373.       
  374.       case WM_DESTROY :
  375.               ImageList_Destroy( TabCtrl_GetImageList( hTab ) );
  376.  
  377.               PostQuitMessage(0);
  378.               break;
  379.  
  380.       default :
  381.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  382.    }
  383.  
  384.    return( FALSE );
  385. }
  386.  
  387.  
  388. LRESULT CALLBACK About( HWND hDlg,           
  389.                         UINT message,        
  390.                         WPARAM wParam,       
  391.                         LPARAM lParam)
  392. {
  393.    switch (message) 
  394.    {
  395.        case WM_INITDIALOG: 
  396.                return (TRUE);
  397.  
  398.        case WM_COMMAND:                              
  399.                if (   LOWORD(wParam) == IDOK         
  400.                    || LOWORD(wParam) == IDCANCEL)    
  401.                {
  402.                        EndDialog(hDlg, TRUE);        
  403.                        return (TRUE);
  404.                }
  405.                break;
  406.    }
  407.  
  408.    return (FALSE); 
  409. }
  410.